package com.dxy.df.core.model;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.dxy.df.constants.ConstStrings;
import com.dxy.df.constants.ErrorEnum;
import com.dxy.df.utils.UcBeanUtils;
import com.fasterxml.jackson.annotation.JsonInclude;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Getter;
import lombok.Setter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;


@Setter
@Getter
@ApiModel
@JsonInclude(JsonInclude.Include.NON_NULL)
public class PageRs<T> implements Serializable {

    private static final long serialVersionUID = 1L;

    private static final Logger LOG = LoggerFactory.getLogger(PageRs.class);

    private Integer code;

    private String msg;

    private boolean success;

    @ApiModelProperty(reference = "paging")
    private Paging paging;


    @ApiModelProperty(reference = ConstStrings.DATA)
    private List<T> data;

    public List<T> getData() {
        return data;
    }



    public Paging getPaging() {
        return paging;
    }



    public PageRs() {
        code = 200;
        msg = ConstStrings.SUCCESS;
        success=true;
    }

    public PageRs(Paging paging, List<T> data) {
        code = 200;
        msg = ConstStrings.SUCCESS;
        success=true;
        this.paging = paging;
        this.data = data;
    }


    public static PageRs ok() {
        return new PageRs();
    }
    public static <T> PageRs ok(Paging paging, List<T> data) {
        PageRs r =  new PageRs();
        r.setPaging(paging);
        r.setData(data);
        return r;
    }


    public PageRs setPaging(Paging paging) {
        this.paging=paging;
        return this;
    }

    public PageRs setData(List<T> data) {
        this.data = data;
        return this;
    }



    public static PageRs error() {
        return error(HttpStatus.INTERNAL_SERVER_ERROR.value(), "未知异常，请联系管理员");
    }

    public static PageRs error(int code, String msg) {
        PageRs r = new PageRs();
        r.setCode(code);
        r.setMsg(msg);
        r.setSuccess(false);
        return r;
    }


    public static PageRs error(ErrorEnum errorEnum) {
        PageRs r = new PageRs();
        r.setCode(errorEnum.getCode());
        r.setMsg(errorEnum.getMsg());
        r.setSuccess(false);
        return r;
    }



    public PageRs<T> putPage(JSONObject result, Class<T> beanClass) {
        if (null == result) {
            return PageRs.error();
        } else if (null != result.getInteger(ConstStrings.CODE)) {
            this.setCode(result.getInteger(ConstStrings.CODE));
            this.setMsg(result.getString(ConstStrings.MSG));
            this.setSuccess(result.getBoolean(ConstStrings.SUCCESS));
            this.setData(getListObject(result.get(ConstStrings.DATA), beanClass));
            try {
                if(null!=result.getJSONObject("paging")) {
                    this.setPaging(UcBeanUtils.mapToObject(result.getJSONObject("paging"), Paging.class));
                }
            } catch (Exception e) {
                LOG.error("接口数据转换异常");
                e.printStackTrace();
            }
        }
        return this;
    }


    private List<T> getListObject(Object data, Class<T> beanClass) {
        List<T> voList = new ArrayList<>();
        if (data instanceof JSONArray) {
            JSONArray array = (JSONArray) data;
            for (Object item : array) {
                if (item instanceof JSONObject) {
                    JSONObject arrayObject = (JSONObject) item;
                    voList.add(JSONObject.toJavaObject(arrayObject, beanClass));
                }
            }
        }
        return voList;
    }




}

